import { defineStore } from 'pinia';
import { getUserInfo } from '@/api/user';
import { clearToken } from '@/utils/auth';
import { UserState } from './types';
// import { data } from './driver';
// import { data } from './admin';

const useUserStore = defineStore('user', {
  state: (): UserState => ({
    realName: '',
    account: '',
    roleNameList: [],
    departments: [],
    type: '',
    email: '',
    carTeamName: [],
    createTime: '',
    avatar: '',
    phone: '',
    userType: 'dispatch',
    driver_id: '',
  }),

  getters: {
    userInfo(state: UserState): UserState {
      return { ...state };
    },
  },

  actions: {
    setInfo(partial: Partial<UserState>) {
      this.$patch(partial);
    },
    resetInfo() {
      this.$reset();
    },
    async getUserInfo() {
      const res = await getUserInfo();
      // const res = data;
      res.data.userType = res.data.tocRole;
      console.log('🚀 ~ getUserInfo ~ res.data.tocRole:', res.data.tocRole);
      // res.data.driver_id = '363213497894912';
      this.setInfo(res.data);
    },
    // Logout
    logout() {
      clearToken();
      window.location.reload();
    },
  },
});

export default useUserStore;
